package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.CardSendService;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.dao.CardSendDao;
import com.qdlc.p2p.dal.dto.CardSend;
import com.qdlc.p2p.dal.model.CardSendModel;

/**
 * Created by lenovo on 2015/12/21.
 */
@Service("cardSendService")
public class CardSendServiceImpl implements CardSendService {

    @Resource
    private CardSendDao cardSendDao;

    @Override
    public PageDataList<CardSendModel> findByModel(CardSendModel model) {
        QueryParam param = QueryParam.getInstance();
        if (!StringUtil.isBlank(model.getSearchName())) {//模糊查询条件
            SearchFilter orFilter1 = new SearchFilter("sendName", SearchFilter.Operators.LIKE, model.getSearchName().trim());
            SearchFilter orFilter2 = new SearchFilter("sendPhone", SearchFilter.Operators.LIKE, model.getSearchName().trim());
            SearchFilter orFilter3 = new SearchFilter("receiveName", SearchFilter.Operators.LIKE, model.getSearchName().trim());
            SearchFilter orFilter4 = new SearchFilter("receivePhone", SearchFilter.Operators.LIKE, model.getSearchName().trim());
            param.addOrFilter(orFilter1, orFilter2, orFilter3, orFilter4);
        } else { //精确查询条件
            if (!StringUtil.isBlank(model.getSendName())) {
                param.addParam("sendName", SearchFilter.Operators.LIKE, model.getSendName().trim());
            }
            if (!StringUtil.isBlank(model.getSendPhone())) {
                param.addParam("sendPhone", SearchFilter.Operators.LIKE, model.getSendPhone().trim());
            }
            if (!StringUtil.isBlank(model.getReceiveName())) {
                param.addParam("receiveName", SearchFilter.Operators.LIKE, model.getReceiveName().trim());
            }
            if (!StringUtil.isBlank(model.getReceivePhone())) {
                param.addParam("receivePhone", SearchFilter.Operators.LIKE, model.getReceivePhone().trim());
            }
            if (!StringUtil.isBlank(model.getReceiveStartTime())) {
                param.addParam("addTime", SearchFilter.Operators.GT, DateUtil.valueOf(model.getReceiveStartTime()));
            }
            if (!StringUtil.isBlank(model.getReceiveEndTime())) {
                Date date = DateUtil.valueOf(model.getReceiveEndTime());
                param.addParam("addTime", SearchFilter.Operators.LT, DateUtil.rollDay(date, 1));
            }
        }
        param.addPage(model.getPage(), model.getRows());
        param.addOrder(OrderType.DESC, "id");
        PageDataList<CardSend> pageDataList = cardSendDao.findPageList(param);
        PageDataList<CardSendModel> pageDataList_ = new PageDataList<CardSendModel>();
        List<CardSendModel> list = new ArrayList<CardSendModel>();
        pageDataList_.setPage(pageDataList.getPage());
        if (pageDataList.getList().size() > 0) {
            for (CardSend cardSend : pageDataList.getList()) {
                CardSendModel experienceModel = CardSendModel.instance(cardSend);
                list.add(experienceModel);
            }
        }
        pageDataList_.setList(list);
        return pageDataList_;
    }

    @Override
    public CardSend save(CardSend cardSend) {
        return cardSendDao.save(cardSend);
    }

    @Override
    public List<CardSend> findByProperty(String property, Object value) {
        return cardSendDao.findByProperty(property, value);
    }

    @Override
    public List<CardSend> findByCriteria(QueryParam param) {
        return cardSendDao.findByCriteria(param);
    }
}
